package com.beiing.tianshuai.tianshuai.mine.ui;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.beiing.tianshuai.tianshuai.R;
import com.beiing.tianshuai.tianshuai.base.BaseActivity;
import com.beiing.tianshuai.tianshuai.entity.CommonBean;
import com.beiing.tianshuai.tianshuai.network.DataManager;
import com.beiing.tianshuai.tianshuai.util.LogUtils;
import com.beiing.tianshuai.tianshuai.util.TimeCount;

import java.text.MessageFormat;

import butterknife.BindView;
import cn.bingoogolapple.bgabanner.BGAOnNoDoubleClickListener;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;

public class SignUpEnterSafeCodeActivity extends BaseActivity {

    @BindView(R.id.toolbar_iv_back)
    ImageView mToolbarIvBack;
    @BindView(R.id.toolbar_tv_title)
    TextView mToolbarTvTitle;
    @BindView(R.id.tv_message)
    TextView mTvMessage;
    @BindView(R.id.et_safe_code)
    EditText mEtSafeCode;
    @BindView(R.id.tv_get_safe_code_again)
    TextView mTvGetSafeCodeAgain;
    @BindView(R.id.btn_next)
    TextView mBtnNext;

    /**
     * 倒计时
     */
    private TimeCount mCountDown;

    /**
     * 当前注册的手机号码
     */
    private String mPhoneNum;

    /**
     * 从服务器端返回的验证码
     */
    private String mSafeCode;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_sign_up_enter_safe_code;
    }

    @Override
    protected void init() {
        Intent intent = getIntent();
        mPhoneNum = intent.getStringExtra("phoneNum");
        mSafeCode = intent.getStringExtra("safeCode");
        mTvMessage.setText(MessageFormat.format("我们已向 {0} 发送验证码短信\n请查看短信并输入验证码", mPhoneNum));

        mCountDown = new TimeCount(60000, 1000, mTvGetSafeCodeAgain, getApplicationContext());
        mCountDown.start();

        initToolbar();
        initListener();
    }

    private void initToolbar() {
        mToolbarIvBack.setVisibility(View.VISIBLE);
        mToolbarIvBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        mToolbarTvTitle.setText("输入验证码");
    }

    private void initListener() {
        // 重新获取验证码
        mTvGetSafeCodeAgain.setOnClickListener(new BGAOnNoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                if (mCountDown != null) {
                    mCountDown.start();
                }
                getSafeCode(mPhoneNum);
            }
        });

        // 下一步
        mBtnNext.setOnClickListener(new BGAOnNoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                String code = mEtSafeCode.getText().toString().trim();
                if (!TextUtils.isEmpty(code)) {
                    if (TextUtils.equals(code, mSafeCode)) {
                        Intent intent = new Intent(mContext, SignUpEnterPasswordActivity.class);
                        intent.putExtra("phoneNum", mPhoneNum);
                        intent.putExtra("safeCode", mSafeCode);
                        startActivity(intent);
                    } else {
                        showShortText("验证码有误");
                    }
                }
            }
        });
    }

    /**
     * 如果获取验证码成功，则跳转到下一个界面。
     *
     * @param phoneNum 用户的手机号码
     */
    private void getSafeCode(final String phoneNum) {
        DisposableObserver<CommonBean> observer = new DisposableObserver<CommonBean>() {
            @Override
            public void onNext(@NonNull CommonBean bean) {
                int code = bean.getCode();
                switch (code) {
                    case 200:
                        mSafeCode = bean.getData().getNumber();
                        showShortText("发送成功");
                        break;
                    case 401:
                        showShortText("获取验证码失败");
                        break;
                    case 400:
                        showShortText("手机号格式不正确");
                        break;
                    default:
                        showShortText("获取验证码失败");
                        break;
                }
            }

            @Override
            public void onError(@NonNull Throwable e) {
                LogUtils.e(e.getMessage(), "error");
                showShortText("请求失败");
            }

            @Override
            public void onComplete() {

            }
        };
        DataManager.getSafeCodeResult(phoneNum)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    @Override
    protected void onDestroy() {
        mCountDown.onFinish();
        mCountDown = null;
        super.onDestroy();
    }
}
